package com.cyd.order_service.service.impl;

import com.cyd.order_service.domain.ProductOrder;
import com.cyd.order_service.service.ProductFeignClient;
import com.cyd.order_service.service.ProductService;
import com.cyd.order_service.utils.JsonUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.netflix.discovery.converters.Auto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.Map;
import java.util.UUID;


@Service
public class ProductServiceImpl implements ProductService {
    private static final Logger log = LoggerFactory.getLogger(ProductServiceImpl.class);
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public ProductOrder save(int userId, int productId) {
        if(userId==1){
            return null;
        }
        //获取商品详情 ribbon(类似包装了httpClient,URLConnection)
//         Map<String,Object> restTemplateForObject=(Map<String, Object>) restTemplate.getForObject("http://product-service/api/product/findById?id="+productId,Object.class);
//        log.info("product object:{}",restTemplateForObject);

        //***********************************loadBalancerClient 方式
//        ServiceInstance instance= loadBalancerClient.choose("product-service");
//        String url=String.format("http://%s:%s/api/product/findById?id="+productId,instance.getHost(),instance.getPort());
//        RestTemplate restTemplate2=new RestTemplate();
////        http://DESKTOP-77SN4J4:8772/api/product/findById?id=3
//        Map<String,Object> restTemplateForObject=(Map<String, Object>) restTemplate2.getForObject(url,Map.class);
//        log.info("product object:{}",restTemplateForObject);
//***********************************loadBalancerClient 方式


        //*************************************Feign start
        String response=productFeignClient.findById(productId);
        log.info(" service saveOrder ......");
        JsonNode jsonNode=JsonUtils.str2JsonNode(response);
        log.info("orderService to  product findById result jsonNode:{}",jsonNode);
        //*************************************Feign end

        ProductOrder  productOrder=new ProductOrder();
        productOrder.setCt(new Date());
        productOrder.setUserId(userId);
        productOrder.setProductId(productId);
//        productOrder.setProductName(restTemplateForObject.get("name").toString());
////        productOrder.setPrice(Integer.parseInt(restTemplateForObject.get("price").toString()));

        productOrder.setProductName(jsonNode.get("name").toString());
        productOrder.setPrice(Integer.parseInt(jsonNode.get("price").toString()));

        productOrder.setTradeCode(UUID.randomUUID().toString());
        return productOrder;

    }
}
